package com.soho.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.jfinal.kit.Kv;

public class TimeHelper {
	
	//基本在trade Aggregations(2016-06-14 为了防止昨天没交易数据,则以一个月到昨天的数据,如果一个月都没有交易,那也没办法了)
	public static Kv getBeforeDays(int day){
		Calendar calendar 	=	Calendar.getInstance();
		int dayOfMonth		=	calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth-day);
		Date _dayDate		=	calendar.getTime();
		
		int month			=	calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month-1);
		Date _monthDate		=	calendar.getTime();
		
		return Kv.by("start_time", atStartOfDay(_monthDate)).set("end_time",atEndOfDay(_dayDate));
	}
	
	//获取之前致今天的数据
	public static Kv getBeforeAndNowDays(int day){
		Calendar calendar 	=	Calendar.getInstance();
		Date _nowTime		=	calendar.getTime();
		
		int dayOfMonth		=	calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth-day);
		Date _dayDate		=	calendar.getTime();
		return Kv.by("start_time", atStartOfDay(_dayDate)).set("end_time",_nowTime.getTime());
	}
	
	public static Kv getNowTime(){
		Calendar calendar 	=	Calendar.getInstance();
		Date _dayDate		=	calendar.getTime();
		return Kv.by("start_time", atStartOfDay(_dayDate)).set("end_time",_dayDate.getTime());
	}
	
	public static long atStartOfDay(Date date) {
	    LocalDateTime localDateTime = dateToLocalDateTime(date);
	    LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
	    return localDateTimeToDate(startOfDay);
	}

	public static long atEndOfDay(Date date) {
	    LocalDateTime localDateTime = dateToLocalDateTime(date);
	    LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
	    return localDateTimeToDate(endOfDay);
	}

	private static LocalDateTime dateToLocalDateTime(Date date) {
	    return LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("Asia/Shanghai"));
	}

	private static long localDateTimeToDate(LocalDateTime localDateTime) {
	    return Date.from(localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant()).getTime();
	}
	
	public static String utcToLocal(String utcTime){
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date utcDate = null;
        try {
            utcDate = sdf.parse(utcTime);
        } catch (ParseException e) {
            PrintHelper.print(e);
        }
       
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(utcDate.getTime());
    }
	
	public static String format(Date date){
		 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date.getTime());
	}
	
	private static SimpleDateFormat ymd =  new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
	public static Kv utcToLocalAndCheckIsToday(String utcTime,String format,String checkDate){
    	
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date utcDate = null;
        try {
            utcDate = sdf.parse(utcTime);
        } catch (ParseException e) {
            PrintHelper.print(e);
            return UtilsHelper.error("parse Error");
        }
        
        String _date		 =	ymd.format(utcDate.getTime());
        if(!_date.equals(checkDate)){
        	return UtilsHelper.error(ContentHelper.OVER_TAG);
        }
        SimpleDateFormat df = new SimpleDateFormat(format);
        return UtilsHelper.success(df.format(utcDate.getTime()));
    }
	
	private static String db_time	=	"yyyy.MM.dd HH:mm";
	private static SimpleDateFormat sdf_time =  new SimpleDateFormat(db_time);
	
	public static long getSecondFromStr(String dateTime) {
		return getSecondFromStr(dateTime, sdf_time);
	}
	public static long getSecondFromStr(String dateTime,SimpleDateFormat formatter) {
		try {
			return formatter.parse(dateTime).getTime()/1000;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public static String formatLocalDateTime(LocalDateTime localDateTime) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(db_time);
        return localDateTime.format(formatter);
	}
	
	public static void main(String[] args) {
	
		String _utcTime	=	"2018-02-02T00:20:10Z";
		String _format	=	"mm:ss";
		String _checkDate=	"2018-02-04";
		Kv _res	=	utcToLocalAndCheckIsToday(_utcTime,_format,_checkDate);
		PrintHelper.print(_res);
	}
}
